package com.future.hedongjun.mine.ui;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import com.flyco.tablayout.SlidingTabLayout;
import com.future.baselib.adapter.FragmentAdapter;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.mine.fragment.DocFragment;
import com.future.hedongjun.mine.fragment.VideoFragment;
import com.future.hedongjun.mine.fragment.VoiceFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class ClassCacheActivity extends AppCompatActivity {

    @BindView(R.id.tabLayout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tv_back)
    TextView tvBack;
    Unbinder unbinder;
    private String[] titles = {"视频", "音频", "课件"};
    private VideoFragment videoFragment;
    private VoiceFragment voiceFragment;
    private DocFragment docFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_class_cache);
        ButterKnife.bind(this);
        ArrayList<Fragment> fragments = new ArrayList<>();
        videoFragment = new VideoFragment(tvBack, ClassCacheActivity.this);
        voiceFragment = new VoiceFragment(tvBack, ClassCacheActivity.this);
        docFragment = new DocFragment(tvBack, ClassCacheActivity.this);
        fragments.add(videoFragment);
        fragments.add(voiceFragment);
        fragments.add(docFragment);
        viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), fragments));
        tabLayout.setViewPager(viewPager, titles);


        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MyApp.isFlag) {
                    switch (viewPager.getCurrentItem()) {
                        case 0:
                            videoFragment.refresh();
                            break;
                        case 1:
                            voiceFragment.refresh();
                            break;
                        case 2:
                            docFragment.refresh();
                            break;
                    }
                    MyApp.isFlag = false;
                } else {
                    finish();
                }
            }
        });
    }
}
